<template>
    <div>
        <content-box
            one="角色管理"
            two="角色列表"
        >
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>
            <el-table
                :data="roleslist"
                border
                stripe
            >
                <!-- 展开列 -->
                <el-table-column type="expand">
                    <template v-slot="{ row }">
                        <!-- 一级权限 -->
                        <el-row
                            v-for="(item1,i1) in row.children"
                            :key="item1.id"
                        >
                            <el-col :span="5">

                                <el-tag
                                    closable
                                    @close="removeRightsById(row,item1.id)"
                                >
                                    {{ item1.authName }}
                                </el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 二级和三级权限 -->
                            <el-col :span="19">
                                <el-row
                                    v-for="(item2,i2) in item1.children"
                                    :key="item2.id"
                                >
                                    <el-col :span="8">
                                        <el-tag
                                            type="danger"
                                            closable
                                            @close="removeRightsById(row,item2.id)"
                                        >
                                            {{ item2.authName }}
                                        </el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <!-- 三级权限 -->
                                    <el-col :span="16">
                                        <el-tag
                                            v-for="(item3,i3) in item2.children"
                                            :key="item3.id"
                                            type="warning"
                                            closable
                                            @close="removeRightsById(row,item3.id)"
                                        >
                                            {{ item3.authName }}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <!-- 索引列 -->
                <el-table-column type="index"></el-table-column>
                <el-table-column
                    label="角色名称"
                    prop="roleName"
                ></el-table-column>
                <el-table-column
                    label="角色描述"
                    prop="roleDesc"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    width="300px"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="primary"
                            icon="el-icon-edit"
                        >编辑</el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            icon="el-icon-delete"
                        >删除</el-button>
                        <el-button
                            size="mini"
                            type="warning"
                            icon="el-icon-setting"
                            @click="openSetRightsDialog(scope.row)"
                        >分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </content-box>

        <!-- 分配权限的弹窗 -->
        <el-dialog
            title="提示"
            :visible.sync="setRightsDialogVisible"
            width="50%"
            @closed="defKeys = []"
        >
            <!-- 务必不能遗漏node-key表示指定节点中的值 -->
            <el-tree
                :data="rightslist"
                show-checkbox
                node-key="id"
                :props="checkedProps"
                default-expand-all
                :default-checked-keys="defKeys"
                ref="treeRef"
            >

            </el-tree>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="setRightsDialogVisible = false">取 消</el-button>
                <el-button
                    type="primary"
                    @click="allotRights"
                >确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
  data() {
    return {
      roleslist: [],
      setRightsDialogVisible: false,
      rightslist: [],
      checkedProps: {
        label: "authName",
        children: "children"
      },
      defKeys: [],
      roleId: ""
    };
  },
  mounted() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get("roles");
      if (res.meta.status !== 200)
        return this.$message.error("获取角色列表失败");
      this.roleslist = res.data;
    },
    async removeRightsById(role, id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "confirm") return this.$message.info("已取消删除");

      // 发起删除权限请求
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${id}`
      );
      if (res.meta.status !== 200) return this.$message.error("删除权限失败");
      // 把接口返回的最新的用户权限赋值给当前用户的children
      // 这样做可以避免刷新页面导致展开行合并
      role.children = res.data;
      this.$message.success("删除权限成功");
    },
    async openSetRightsDialog(role) {
      this.roleId = role.id;
      this.setRightsDialogVisible = true;
      // 获取所有的权限用于展示
      const { data: res } = await this.$http.get("rights/tree");
      this.rightslist = res.data;
      this.getLeafKeys(role, this.defKeys);
    },
    // 通过递归获取角色对象下 所有的三级权限的id
    getLeafKeys(node, arr) {
      // 结束条件
      if (!node.children) {
        return arr.push(node.id);
      }
      // 继续下探
      node.children.forEach(item => this.getLeafKeys(item, arr));
    },
    // 为角色分配权限
    // 参数 -- 角色id 当前角色下所有的权限id
    async allotRights() {
        // 获取角色下所有的权限id
        let rids = [ ...this.$refs.treeRef.getHalfCheckedKeys(),...this.$refs.treeRef.getCheckedKeys()]
        rids = rids.toString()
        const { data:res } = await this.$http.post(`roles/${ this.roleId }/rights`,{ 
            rids
         })
         if(res.meta.status !== 200) return this.$message.error('分配权限失败')
         this.$message.success('分配权限成功')
         this.getRolesList()
         this.setRightsDialogVisible = false
    }
  }
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
</style>